package jgl.vajra.operator.part.login.activity;

import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.LoginInfoBean;
import jgl.vajra.operator.databinding.ActivityBindMachineBossBinding;
import jgl.vajra.operator.manager.AccountManager;
import jgl.vajra.operator.manager.IntentManager;
import jgl.vajra.operator.part.login.contract.BindMachineBossContract;
import jgl.vajra.operator.part.login.viewModel.BindMachineBossViewModel;
import jgl.vajra.operator.utils.CountUtils;
import jgl.vajra.operator.utils.RandomUtils;
import jgl.vajra.operator.widget.manager.SPManager;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.operator.widget.utils.ToastUtils;
import jgl.vajra.operator.widget.utils.UIUtils;

@CreateViewModel(BindMachineBossViewModel.class)
public class BindMachineBossActivity extends BaseMVVMActivity<BindMachineBossViewModel, ActivityBindMachineBossBinding> implements BindMachineBossContract.View {

    private boolean type;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bind_machine_boss;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        type = getIntent().getBooleanExtra("type", false);

    }

    //获取验证码
    @Override
    public void onVerifyCode() {
        String phone = mBinding.etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入正确的电话号码");
            return;
        }
        getVerifyCode();
    }

    public void getVerifyCode() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(mBinding.etPhone.getText().toString().trim() + "&&" + RandomUtils.getRandom()));
        map.put("type", "register");
        mViewModel.getVerifyCode(map);
    }

    @Override
    public void onDetermine() {
        String phone = mBinding.etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入正确的电话号码");
            return;
        }

        String verifyCode = mBinding.etVerifyCode.getText().toString().trim();
        if (TextUtils.isEmpty(verifyCode)) {
            ToastUtils.showShort("请输入验证码");
            return;
        }


        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("phone", AEACCoder.encrypt(phone));
        map.put("verificationCode", verifyCode);
        mViewModel.bindMachineData(map);
    }

    @Override
    public void returnBindMachineData() {
        ToastUtils.showShort("绑定成功");
        if(type){
            SPManager.LoginData.setIsLogin(true);
            IntentManager.toHomeActivity(BindMachineBossActivity.this);
        }else{
            LoginInfoBean loginData = AccountManager.getInstance().getLoginData(UIUtils.getContext());
            loginData.ownerPhone=AEACCoder.encrypt(mBinding.etPhone.getText().toString().trim());
            AccountManager.getInstance().saveLoginData(this, loginData);
        }
        finish();
    }

    @Override
    public void returnVerifyCodeData(int code) {
        if (code==200){
            CountUtils.getCount(mBinding.tvVerifyCode);
        }else {
            mBinding.tvVerifyCode.setClickable(true);
        }
    }
}
